package com.mobi.Survey;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.mobi.Survey.controller.SubscriptionPojo;
import com.mobi.Survey.controller.SurveyPojo;
import com.mobi.Survey.model.SurveyTable;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * SurveyInformation.java This class is responsible for displaying information
 * about survey
 */
public class SurveyInformation extends AbstractScreen
{
	private TextView txtCompanyName;
	private TextView txtPhoneNo;

	private TextView txtCntcEmail;
	private TextView txtWebsite;
	private TextView txtSubsBy;
	private TextView txtSubsEmail;

	private long surveyId;
	private SurveyPojo survey = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{

		super.onCreate(savedInstanceState);
		hideKeaboard = true;

		Intent intent = getIntent();
		if (intent != null)
		{
			Bundle bundle = intent.getExtras();
			if (bundle != null)
			{
				SurveyTable stable = new SurveyTable(this);
				// Survey id of survey for which display information should be
				// greater than 0
				surveyId = ((Long) bundle.get("data")).intValue();
				try
				{
					survey = stable.getSurveyForInformationScreen(this,
							surveyId);
				} catch (Exception e)
				{

					showShortToast(Messages.NO_SURVEY_INFO_AVAILABLE);
					finish();
				} finally
				{
					stable.closeDB();
				}

				setTitle(survey.getSurveyDiscription());
			}

		}

		txtCompanyName.setText(" " + survey.getCompanyName());

		if (survey.getPhone() != null && survey.getPhone().length() > 0)
			txtPhoneNo.setText(" " + survey.getPhone());

		if (survey.getContactEmail() != null
				&& survey.getContactEmail().length() > 0)
			txtCntcEmail.setText(" " + survey.getContactEmail());

		if (survey.getWebsite() != null && survey.getWebsite().length() > 0)
			txtWebsite.setText(" " + survey.getWebsite());

		txtSubsBy.setText(" " + survey.getSubscription().getfName() + " "
				+ survey.getSubscription().getlName());

		if (survey.getSubscription().getEmail() != null
				&& survey.getSubscription().getEmail().length() > 0)
			txtSubsEmail.setText(" " + survey.getSubscription().getEmail());

		if (survey.getSubscription().getAddress().isAddressNull())
		{
			findViewById(R.id.map).setVisibility(View.GONE);
		} else
		{
			SubscriptionPojo sub = survey.getSubscription();

			String query = ' ' + sub.getAddress().getStreetNumber() + ", "
					+ sub.getAddress().getStreetName() + ", "
					+ sub.getAddress().getCity() + ", "
					+ sub.getAddress().getState() + ", "
					+ sub.getAddress().getCountry() + ", "
					+ sub.getAddress().getZip();
			((TextView) findViewById(R.id.tvAddress)).setText(query);
		}

	}

	private void openMap()
	{
		try
		{
			SubscriptionPojo sub = survey.getSubscription();
			String query = sub.getAddress().getStreetNumber() + "+"
					+ sub.getAddress().getStreetName() + "+"
					+ sub.getAddress().getCity() + "+"
					+ sub.getAddress().getState() + "+"
					+ sub.getAddress().getCountry() + "+"
					+ sub.getAddress().getZip();
			Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
					Uri.parse("geo:0,0?q=" + query));
			startActivity(intent);
		} catch (Exception e)
		{
			Toast.makeText(this, Messages.NOTHING_FOUND_TO_COMPLETE_ACTION,
					Toast.LENGTH_LONG).show();
		}

	}

	public void textClick(View view)
	{

		if (view.getId() == R.id.phone)
		{
			makeCall(txtPhoneNo.getText().toString());
		} else if (view.getId() == R.id.ContactEmail)
		{
			sendEmail(txtCntcEmail.getText().toString(),
					survey.getSurveyDiscription());
		} else if (view.getId() == R.id.Website)
		{
			openUrl(txtWebsite.getText().toString());
		} else if (view.getId() == R.id.email)
		{
			sendEmail(txtSubsEmail.getText().toString(),
					survey.getSurveyDiscription());
		} else if (view.getId() == R.id.map)
		{
			openMap();
		}
	}

	@Override
	protected void initUiVars()
	{
		txtCompanyName = (TextView) findViewById(R.id.tvCompanyName);
		txtPhoneNo = (TextView) findViewById(R.id.tvPhone);

		txtCntcEmail = (TextView) findViewById(R.id.tvContactEmail);
		txtWebsite = (TextView) findViewById(R.id.tvWebsite);
		txtSubsBy = (TextView) findViewById(R.id.tvSubsBy);
		txtSubsEmail = (TextView) findViewById(R.id.tvEmail);

	}

	@Override
	protected int getLayoutId()
	{

		return LayoutIds.SURVEY_IFNO_SCREEN;
	}

	@Override
	protected void onRelease()
	{

	}

}
